package com.xiaoq.matrix.minipro.endpoint;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.JsonUtil;
import com.xiaoq.matrix.minipro.entity.ShopMiniProEntity;
import com.xiaoq.matrix.minipro.service.impl.MiniProQlCodeServiceImpl;
import com.xiaoq.matrix.minipro.service.impl.MiniProServiceImpl;
import com.xiaoq.matrix.minipro.vo.MiniProAuditStatusResponse;
import com.xiaoq.matrix.minipro.vo.MiniProBaseResponse;
import com.xiaoq.matrix.minipro.vo.MiniProCategory;
import com.xiaoq.matrix.minipro.vo.MiniProCategoryListResponse;
import com.xiaoq.matrix.minipro.vo.MiniProDraftOrTemplateListResponse;
import com.xiaoq.matrix.minipro.vo.MiniProPageListResponse;
import com.xiaoq.matrix.minipro.vo.MiniProSubmitAuditResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Controller
@RequestMapping("/api/v1/superMgr")
public class MiniProSuperMgrEndpoint {
	@Autowired
	private  MiniProServiceImpl miniProService;
	
	@ApiOperation(value = "获取小程序上线二维码", notes = "获取小程序上线二维码")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取小程序上线二维码") })
	@GetMapping(value = "/minicode", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getQlCode(@RequestParam(value="accessToken",required=true)String accessToken,@RequestParam(value="path",required=true)@ApiParam(value="小程序对应的路径")String path){
		String base64QlCode = MiniProQlCodeServiceImpl.getminiqrQr(accessToken,path);
		return GenericDataResponse.okWithData(base64QlCode);
		
		
	}
	
	@ApiOperation(value = "根据ShopId获取二维码", notes = "获取小程序上线二维码")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取小程序上线二维码") })
	@GetMapping(value = "/miniQrcode", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getQlCodeByshopId(@RequestParam(value="shopId",required=true)String shopId,@RequestParam(value="path",required=true)@ApiParam(value="小程序对应的路径")String path){
		if("pages/index/index".equals(path)){
			String qrImage = miniProService.getShopMiniProQrImage(shopId, "2");
			return GenericDataResponse.okWithData(qrImage);
		}
		String accessToken = miniProService.getAccessTokenByShopId(shopId, "2");
		String base64QlCode = MiniProQlCodeServiceImpl.getminiqrQr(accessToken,path);
		miniProService.updateQrImage(shopId, "2", base64QlCode);
		return GenericDataResponse.okWithData(base64QlCode);
		
		
	}
	
	@ApiOperation(value = "添加体验二维码的体验者", notes = "添加体验二维码的体验者")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加体验二维码的体验者") })
	@PostMapping(value = "/axperiencers", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addExperiencer(
			@RequestParam(value="wechatid",required=true)@ApiParam(value="体验者微信号")String wechatid,
			@RequestParam(value="accessToken",required=true)String accessToken){
		MiniProBaseResponse miniPro = miniProService.addExperiencer(accessToken,wechatid);
		return GenericDataResponse.okWithData(miniPro);
		
		
	}
	

	@ApiOperation(value = "获取体验二维码(全网发布之前添加体验者才可以扫二维码体验)", notes = "获取体验二维码")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取体验二维码") })
	@GetMapping(value = "/qrCode", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getQRCode(@RequestParam(value="accessToken",required=true)String accessToken) {
		String miniPro = miniProService.getQRCodeFromWeixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);

	}
	
	@ApiOperation(value = "获取刷新后的token", notes = "获取刷新厚的token")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取刷新厚的token") })
	@GetMapping(value = "/refresh", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> refreshToken(@RequestParam(value="shopId",required=true)String shopId) {
		ShopMiniProEntity shopMiniProEntity = miniProService.refreshAccessToken(shopId,"2");
		String accessToken = shopMiniProEntity.getAuthorizerInfo().getAuthorizerAccessToken();
		return GenericDataResponse.okWithData(accessToken);
		
		
	}
	
	
	@ApiOperation(value = "发布小程序", notes = "发布小程序")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericDataResponse.class, message = "跳转链接")})
	@PostMapping(value = "/publish")
	public ResponseEntity<?> submitAudit(String accessToken) {
		MiniProBaseResponse miniPro = miniProService.release2Weixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
	}
	
	@ApiOperation(value = "获取草稿箱内的草稿Id", notes = "测试更换主图媒体Id")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "测试更换主图媒体Id") })
	@GetMapping(value = "/deaftId", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getgetTemplateDraftListFromWeixin(
			@RequestParam(value="accessToken",required=true)String accessToken) {
		MiniProDraftOrTemplateListResponse miniPro = miniProService.getTemplateDraftListFromWeixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
	}
	
	@ApiOperation(value = "将草稿箱里的版本添加到模板库", notes = "将草稿箱里的版本添加到模板库")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "将草稿箱里的版本添加到模板库") })
	@PostMapping(value = "/temPlate", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addTemplate2Weixin(
			@RequestParam(value="accessToken",required=true)String accessToken, 
			@RequestParam(value="draftId",required=true)Long draftId){
		MiniProBaseResponse miniPro = miniProService.addTemplate2Weixin(accessToken, draftId);
		return GenericDataResponse.okWithData(miniPro);
		
		
	}
	
	@ApiOperation(value = "获取授权小程序帐号的可选类目", notes = "获取授权小程序帐号的可选类目")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取授权小程序帐号的可选类目") })
	@GetMapping(value = "/categoryList", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getCategoryListFromWeixin(
			@RequestParam(value="accessToken",required=true)String accessToken){
		MiniProCategoryListResponse miniPro = miniProService.getCategoryListFromWeixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
	}
	
	@ApiOperation(value = "获取小程序的第三方提交代码的页面配置", notes = "获取小程序的第三方提交代码的页面配置")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取小程序的第三方提交代码的页面配置") })
	@GetMapping(value = "/pageList", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getPageConfFromWeixin(
			@RequestParam(value="accessToken",required=true)String accessToken){
		MiniProPageListResponse miniPro = miniProService.getPageConfFromWeixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
		
		
	}
	
	@ApiOperation(value = "程序提交审核", notes = "程序提交审核")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "程序提交审核") })
	@PostMapping(value = "/submit", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> submitAudit2Weixin(
			@RequestParam(value="accessToken",required=true)String accessToken,
			@ModelAttribute MiniProCategory miniProCategory){
		List<MiniProCategory> items = new ArrayList<MiniProCategory>();
		items.add(miniProCategory);
		MiniProSubmitAuditResponse miniPro = miniProService.submitAudit2Weixin(accessToken,items );
		return GenericDataResponse.okWithData(miniPro);
		
	}
	@ApiOperation(value = "获取最后一次审核结果", notes = "获取最后一次审核结果")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取最后一次审核结果") })
	@GetMapping(value = "/submitStatusa", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getAuditStatusFromWeixinfora(String accessToken){
		MiniProAuditStatusResponse miniPro = miniProService.getLastAuditStatusFromWeixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
	}
	
	@ApiOperation(value = "添加服务器域名", notes = "添加服务器域名")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加服务器域名") })
	@PostMapping(value = "/severDomain", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addAdmain(String accessToken){
		MiniProBaseResponse miniPro = miniProService.saveDomainConf2Weixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
	}
	
	@ApiOperation(value = "添加小程序业务域名", notes = "添加小程序业务域名")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加小程序业务域名") })
	@PostMapping(value = "/severwebview", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addWebview(String accessToken){
		MiniProBaseResponse miniPro = miniProService.saveWebviewConf2Weixin(accessToken);
		return GenericDataResponse.okWithData(miniPro);
		
		
	}
	
	@ApiOperation(value = "添加商户号和商户秘钥（认证完成后才能晚上商户号和商户秘钥）", notes = "添加商户号和商户秘钥")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加商户号和商户秘钥") })
	@PostMapping(value = "/secrets", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addMiniProSecret(@RequestParam String appId,@RequestParam String appSecret){
		boolean result = miniProService.addSecret(appId, appSecret);
		if(result){
			return GenericResponse.ok();
		}
		return GenericDataResponse.ng("小程序认证后才能添加商户号！");
		
		
	}

}
